﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using My.Utils;
using System.Threading;

namespace EmailStorm.App
{
    public enum ProgressStatus { 
        Info=1,
        Error=2
    }
    
    public delegate void UpdateProgress(string status,string message);
    public class SearchThread
    {
        public DataTable EmailList;
        public SearchTask Task;
        public UpdateProgress OnUpdateProgress;
        public Thread Thread;
        public bool Exit = false;
        public string Keyword;
        public int PageIndex=0;
        public int PageCount = 1;
        public void StartSearch() {
            this.OnUpdateProgress("init", "init");

            while (this.PageIndex < this.PageCount) {
                if (this.Exit)
                { //应用程序退出
                    return;
                }
                this.PageCount = SearchInPage(this.PageIndex);
                this.PageIndex++;


                Data.Task obj = new EmailStorm.Data.Task();
                obj.UpdateTaskInfo(this.Task.TaskId, this.PageIndex, this.PageCount);


                Thread.Sleep(100);
            }
            
        }

        public int SearchInPage(int pageIndex) {
            this.OnUpdateProgress("page", "page"+pageIndex.ToString());
            string url = "http://www.google.com/search?hl=en&lr=lang_en&num=20&start={0}&newwindow=1&q={1}+%40gmail.com";
            url = string.Format(url, pageIndex * 20,this.Keyword.Replace(" ","+"));
            My.Utils.MyHttp http= new My.Utils.MyHttp();
            string result=http.Get(url);
            Match m=Regex.Match(result,@"Results <b>\d+</b> - <b>\d+</b> of about <b>([\d,]+)</b>");
            string s_recordCount=m.Groups[1].Value;
            s_recordCount=s_recordCount.Replace(",", "");
            int recordCount = Int32.Parse(s_recordCount);
            int pageCount = recordCount / 20;  


            string pattern="<h3 class=r><a href=\"(.+?)\".+?>(.+?)</a></h3>";
            MatchCollection mc=Regex.Matches(result, pattern);
            foreach (Match m1 in mc) {
                string pageUrl=m1.Groups[1].Value;
                string title = My.Utils.StringText.Html2Text(m1.Groups[2].Value);
                SearchInDetail(pageUrl);
            }

            return pageCount;

        }

        public void SearchInDetail(string url) {
            Data.Email obj = new EmailStorm.Data.Email();
            
            My.Utils.MyHttp http=new MyHttp();
            string result = http.Get(url);
            string pattern = @"[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}";
            MatchCollection mc=Regex.Matches(result, pattern);
            foreach (Match m in mc) {
                string email=m.Value;
                DataRow row=EmailList.NewRow();
                row[0] = email;
                row[1] = url;
                this.Task.AddRow(row);
                //this.EmailList.Rows.Add(row);
                //this.EmailList.Rows.InsertAt(row,0);
                this.OnUpdateProgress("email", email);
                obj.AddEmail(email, email, url, url, this.Task.TaskId);

            }
        }


    }
}
